﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Configuration;

namespace Tab2Sql
{
    /// <summary>
    ///
    /// </summary>
    public class SqlCmd
    {
        
        private  string sqlCmdExe = Properties.Settings.Default.sqlCmdPath;
        private string stdout;
        private string stderror;
        /// <summary>
        ///
        /// </summary>
        /// <param name="filePath">
        /// A <see cref="System.String"/>
        /// </param>
        public SqlCmd (string filePath)
        {
            stdout = String.Empty;
            LaunchSqlCmd (filePath);
        }
        /// <summary>
        ///
        /// </summary>
        public string Stderror
        {
            get { return stderror; }
        }
        /// <summary>
        ///
        /// </summary>
        public string Stdout
        {
            get { return stdout; }
        }

        private void LaunchSqlCmd(string filePath)
        {
            var myP = new Process();
            myP.StartInfo.FileName = sqlCmdExe;

            string myServer = Properties.Settings.Default.DBServer;
            string myUserID = Properties.Settings.Default.DBUserID;
            string myPassword = Properties.Settings.Default.DBPassword;
            string myDB = Properties.Settings.Default.DBName;

            string myArguments = "-S"+myServer+" -U"+myUserID+" -P"+myPassword + " -d"+myDB;
            myArguments += " -i \"" + filePath + "\"";

            myP.StartInfo.Arguments = myArguments;
            myP.StartInfo.UseShellExecute = false;
            myP.StartInfo.CreateNoWindow = true;
            myP.StartInfo.RedirectStandardOutput = true;
            myP.StartInfo.RedirectStandardError = true;
            myP.StartInfo.RedirectStandardInput = true;
            myP.Start();
            StreamReader myStreamReader = myP.StandardOutput;
            StreamReader myStreamReader2 = myP.StandardError;

            // Reads a single line of the programs output.
            string myString = myStreamReader.ReadToEnd();
            
            stdout = myString;

            string myError = myStreamReader2.ReadToEnd();
            stderror = myError;
            /*
             * Copyright (c) Microsoft Corporation.  All rights reserved.
                usage: Sqlcmd            [-U login id]          [-P password]
                  [-S server]            [-H hostname]          [-E trusted connection]
                  [-d use database name] [-l login timeout]     [-t query timeout]
                  [-h headers]           [-s colseparator]      [-w screen width]
                  [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
                  [-c cmdend]            [-L[c] list servers[clean output]]
                  [-q "cmdline query"]   [-Q "cmdline query" and exit]
                  [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
                  [-u unicode output]    [-r[0|1] msgs to stderr]
                  [-i inputfile]         [-o outputfile]        [-z new password]
                  [-f <codepage> | i:<codepage>[,o:<codepage>]] [-Z new password and exit]
                  [-k[1|2] remove[replace] control characters]
                  [-y variable length type display width]
                  [-Y fixed length type display width]
                  [-theTableName[1] print statistics[colon format]]
                  [-R use client regional setting]
                  [-b On error batch abort]
                  [-v var = "value"...]  [-A dedicated admin connection]
                  [-X[1] disable commands, startup script, enviroment variables [and exit]]
                  [-x disable variable substitution]
                  [-? show syntax summary]*/
        }
    }
}
